#!/usr/bin/env bash
set -x
SELFDIR=$(cd $(dirname "${BASH_SOURCE[0]}");pwd)

#   HOST_ARCH       主机硬件架构(x85_64、aarch64)
#   HOST_PLATFORM   主机内核(winnt、linux、darwin、msys)
HOST_ARCH=$(arch | tr '[:upper:]' '[:lower:]')
HOST_PLATFORM=$(uname -s | sed 's#^\([^-]\+\)-.*#\1#g' | tr '[:upper:]' '[:lower:]')
if   [[ "${HOST_PLATFORM}" == "mingw64_nt" ]]; then
    HOST_PLATFORM="msys"
elif [[ "${HOST_PLATFORM}" == "" ]]; then
    HOST_PLATFORM="winnt"
fi

if   [[ "${HOST_PLATFORM}" == "darwin" ]]; then
    if [[ "${HOST_ARCH}" == "i386" ]]; then
        HOST_ARCH="x86_64"
    fi
fi

#   $1  go_install_path
function install_go()
{
    #   go 的安装路径
    local go_install_path="$1"

    #   如果已经安装了
    if [[  -f "${go_install_path}/bin/go" ]]; then
        return  0
    fi

    #   如果没有安装就需要安装上去

    #   确定依赖的go的版本号
    local go_label=$(cat "${SELFDIR}/go-version")
    if [[ "${go_label}" == "" ]]; then
        echo    "Dependency go compiler is not specified: '${SELFDIR}/go-version'"
        return  1
    fi

    local gopkg_arch="${HOST_ARCH}"
    local gopkg_platform="${HOST_PLATFORM}"
    local gopkg_suffix=".tar.gz"
    if [[ "${gopkg_arch}" == "x86_64" || "${gopkg_arch}" == "i386" ]]; then
        gopkg_arch="amd64"
    fi
    if [[ "${gopkg_platform}" == "msys" ]]; then
        gopkg_platform="windows"
        gopkg_suffix=".zip"
    fi

    #   确定go的包是否存在
    local go_package=$(cat "${PKGREPO}/${go_label}/VERSION" | grep ":major:" | grep ":${HOST_ARCH}:" | grep ":${HOST_PLATFORM}:" | awk '{print $3}')
    if [[ ! -f "${PKGREPO}/${go_label}/${go_package}" ]]; then
        echo    "The required go compiler package is not exist: '${PKGREPO}/${go_label}/${go_package}'"
        return  1
    fi

    #   清理 go 安装包的位置
    rm -rf   "${go_install_path}-unzip"
    mkdir -p "${go_install_path}-unzip"
    RESULT=$?
    if [[ ${RESULT} -ne 0 ]]; then
        echo    "Create directory for go compiler failed(${RESULT})"
        return  1
    fi

    if   [[ "${gopkg_suffix}" == ".tar.gz" ]]; then
        tar xvfz "${PKGREPO}/${go_label}/${go_package}" -C "${go_install_path}-unzip"
        RESULT=$?
    elif [[ "${gopkg_suffix}" == ".zip" ]]; then
        cd "${go_install_path}-unzip" && \
        unzip "${PKGREPO}/${go_label}/${go_package}"
        RESULT=$?
    else
        echo    "Decompress package failed, unsupported package suffix: '${PKGREPO}/${go_label}/${go_package}'"
        return  1
    fi
    if [[ ${RESULT} -ne 0 ]]; then
        echo    "Decompress package failed(${RESULT}): '${PKGREPO}/${go_label}/${go_package}'"
        return  1
    fi

    rm -rf  "${go_install_path}"
    mv -f   "${go_install_path}-unzip/go"   "${go_install_path}"
    RESULT=$?
    if [[ ${RESULT} -ne 0 || ! -f "${go_install_path}/bin/go" ]]; then
        echo    "Install go compiler failed: '${PKGREPO}/${go_label}/${go_package}' -> '${go_install_path}'"
        return  1
    fi

    rm -rf "${go_install_path}-unzip"

    return  0
}

#   $1    pkgr 安装到什么地方，如果没指定就安装到当前位置
function main()
{
    local pkgr_install_dir="$1"
    if [[ "${pkgr_install_dir}" == "" ]]; then
        pkgr_install_dir="."
    fi

    #   纠正成绝对路径
    pkgr_install_dir=$(realpath -m -- "${pkgr_install_dir}")

    #   先确保 go 已经安装到本地了
    local go_install_path="${SELFDIR}/go-${HOST_ARCH}-${HOST_PLATFORM}"
    install_go  "${go_install_path}"
    RESULT=$?
    if [[ ${RESULT} -ne 0 ]]; then
        return  1
    fi

    #   准备好安装目录
    if [[ ! -e "${pkgr_install_dir}/bin" ]]; then
        mkdir -p "${pkgr_install_dir}/bin"
    fi
    rm -rf "${pkgr_install_dir}/bin/pkgr-${HOST_ARCH}"

    #   安装新包
    export  PATH="${go_install_path}/bin:${PATH}"
    export  GOROOT="${go_install_path}"
    cd "${SELFDIR}/src" && \
    go version          && \
    go build -o "${pkgr_install_dir}/bin/pkgr-${HOST_ARCH}"
    RESULT=$?
    if [[ ${RESULT} -ne 0 || ! -f "${pkgr_install_dir}/bin/pkgr-${HOST_ARCH}" ]]; then
        echo    "Build pkgr failed(${RESULT})"
        return  1
    fi

    return  0
}

main    "$@"
exit    "$?"
